package com.lucene.analyzer;
import com.hankcs.lucene.HanLPTokenizerFactory;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.util.TokenizerFactory;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by zhangzhen on 16/10/2017.
 */
public class CustomizedAnalyzer extends Analyzer {
    private static Logger logger = Logger.getLogger(CustomizedAnalyzer.class);
    private boolean enablePorterStemming;
    private Set<String> filter;
    private String stopWordPath;
    private String userDictPath;

    public CustomizedAnalyzer(Set<String> filter, boolean enablePorterStemming) {
        this.filter = filter;
        this.enablePorterStemming = enablePorterStemming;
    }

    public CustomizedAnalyzer(boolean enablePorterStemming) {
        this.enablePorterStemming = enablePorterStemming;
    }

    public CustomizedAnalyzer(String stopWordPath, String userDictPath, boolean enablePorterStemming) {
        this.stopWordPath = stopWordPath;
        this.userDictPath = userDictPath;
        this.enablePorterStemming = enablePorterStemming;
    }

    public CustomizedAnalyzer() {
        this.stopWordPath = "dict/stopwords.txt";
        this.userDictPath = "dict/user.dict";
    }

    protected TokenStreamComponents createComponents(String fieldName) {
        Map<String, String> args = new TreeMap<>();
        args.put("enableTraditionalChineseMode", "true");
        args.put("customDictionaryPath", this.userDictPath);
        args.put("stopWordDictionaryPath", this.stopWordPath);

        TokenizerFactory factory = new HanLPTokenizerFactory(args);
        Tokenizer tokenizer = factory.create(null);
        logger.info("run createComponents");
        // Tokenizer tokenizer = new HanLPTokenizer(HanLP.newSegment().enableOffset(true), this.filter, this.enablePorterStemming);
        return new TokenStreamComponents(tokenizer);
    }
}
